<template>
  <div class="employee-container">
    <!-- 搜索区域 -->
    <!--
      搜索功能：前端吧当前用户输入的关键词传给后端 后端用这个关键词做过滤筛选 把符合条件的返回给前端
      1.双向绑定拿到关键词
      2.使用最新的关键词字段数据重新获取列表

      搜索： 1.精确搜索（name完全等于用户名字才可以搜索） 2.模糊匹配（只要name中包含即可不需要精确搜索）
       -->
    <div class="search-container">
      <div class="search-label">企业名称:</div>
      <el-input v-model="params.enterpriseName" clearable placeholder="请输入企业名称" class="search-main" @clear="clearSearch" />
      <div class="search-label">缴费时间:</div>
      <el-date-picker
        v-model="params.start"
        type="daterange"
        range-separator="-"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      />
      <el-button type="primary" @click="query">查询</el-button>
    </div>
    <div class="create-container">
      <el-button type="primary" @click="add">添加企业</el-button>
    </div>
    <!-- 表格区域 -->
    <div class="table">
      <el-table style="width: 100%" :data="PropetyList">
        <el-table-column type="index" label="序号" />
        <el-table-column label="账单编号" width="180" prop="billNumber" />
        <el-table-column label="企业名称" width="180" prop="enterpriseName" />
        <el-table-column label="租赁楼宇" width="180" prop="buildingName" />
        <el-table-column label="物业费(元/㎡)" width="180" prop="propertyFeePrice" />
        <el-table-column label="账单金额(元)" width="180" prop="paymentAmount" />
        <el-table-column label="缴费时间" prop="startTime" />
        <el-table-column label="操作" fixed="right" width="180">
          <template #default="scope">
            <el-button size="mini" type="text">编辑</el-button>
            <el-button size="mini" type="text" @click="deletePropetyList(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="page-container">
      <!-- 分页 -->
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="params.pageSize"
        @current-change="pageChange"
      />
      <!-- 表单接口 -->
      <el-dialog
        :title="id?'编辑员工':'添加员工'"
        :visible="dialogVisible"
        width="480px"
      >
        <!-- 表单接口 -->
        <div class="form-container">

          <el-form ref="addForm" :model="addForm" :rules="addFormRules">
            <el-form-item label="选择租户">
              <el-select v-model="addForm.enterpriseId" placeholder="请选择租户" style="width:100%">
                <el-option v-for="item in enterpriseList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>

            <el-form-item label="租赁楼宇">
              <el-select v-model="addForm.buildingId" placeholder="请选择租赁楼宇" style="width:100%">
                <el-option v-for="item in buildingList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>

            <el-form-item label="缴费周期" prop="phonenumber">
              <el-date-picker
                v-model="addForm.paytime"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              />
            </el-form-item>

            <el-form-item label="支付金额" prop="roleId">
              <el-input v-model="addForm.paymentAmount" disabled />
            </el-form-item>

            <el-form-item label="支付方式" prop="userName">
              <el-select v-model="addForm.paymentMethod" placeholder="请选择支付方式">
                <el-option label="现金" value="现金" />
                <el-option label="微信" value="微信" />
                <el-option label="支付宝" value="支付宝" />
              </el-select>
            </el-form-item>

          </el-form>
        </div>
        <template #footer>
          <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
          <el-button size="mini" type="primary" @click="addPropetyList">确 定</el-button>
        </template>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { getPropetyListAPI, deletePropetyListAPI, addPropetyListAPI, getBuildingAPI, getEnterpriseAPI, getPaymentAPI } from '@/api/propety'
export default {
  name: 'Proprty',
  data() {
    return {
      // 楼宇列表
      buildingList: [],
      // 公司列表
      enterpriseList: [],
      dialogVisible: false,
      id: '',
      total: 50,
      params: {
        enterpriseName: '', // 企业名称
        start: '', // 起始时间-缴费时间
        end: '', // 截止时间-缴费时间
        page: 1,
        pageSize: 10
      },
      PropetyList: [],
      // 表单
      addForm: {
        enterpriseId: '', // 企业id
        buildingId: '', // 楼宇id
        paytime: [], // 缴费周期 结束时间
        paymentAmount: '', // 付款总额
        paymentMethod: ''// 付款方式
      },
      // 验证规则
      addFormRules: {
        enterpriseId: [
          { required: true, message: '请选择租户', trigger: 'blur' }
        ],
        buildingId: [
          { required: true, message: '请选择租赁楼宇', trigger: 'blur' }
        ],
        paytime: [
          { required: true, message: '请选择缴费周期', trigger: 'blur' }
        ],
        paymentAmount: [
          { required: true, message: '请输入支付金额', trigger: 'blur' }
        ],
        paymentMethod: [
          { required: true, message: '请选择支付方式', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    addForm: {
      // 深度监视
      deep: true,
      async handler(obj) {
        // 如果填写了楼宇和缴费周期，计算物业费
        if (obj.buildingId && obj.paytime.length !== 0) {
          const { buildingId, paytime } = obj
          const res = await getPaymentAPI({
            buildingId, paytime
          })
          this.addForm.paymentAmount = res.data
        }
      }
    }

  },
  mounted() {
    this.getPropetyList()
  },
  methods: {
    async getPropetyList() {
      const res = await getPropetyListAPI(this.params)
      this.PropetyList = res.data.rows
      console.log(this.PropetyList)
    },
    pageChange(page) {
      this.params.page = page
      this.getPropetyList()
    },
    // 查询
    query() {
      console.log(11)
      this.getPropetyLists()
    },
    // 删除
    async deletePropetyList(id) {
      this.$confirm('删除员工后将不可登录，确认删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deletePropetyListAPI(id)
        this.$message({
          type: 'success',
          message: '删除成功'
        })
        this.PropetyList()
      })
    },
    // 添加
    addPropetyList() {
      this.$refs.addForm.validate(async(valid) => {
        if (valid) {
          // 组装数据
          const { enterpriseId, buildingId, paytime, paymentAmount, paymentMethod } = this.addForm
          const data = { enterpriseId, buildingId, paymentAmount, paymentMethod,
            rentTime: paytime[0],
            endTime: paytime[1]
          }
          // 发送添加请求
          await addPropetyListAPI(data)
          this.$message.success('添加成功')
          this.dialogVisible = false
          this.getPropetyList()
        }
      })
    },
    add() {
      this.dialogVisible = true
      this.getBuilding()
      this.getEnterprise()
    },
    clearSearch() {
      // 把当前的请求页数 复原为1 然后再请求
      this.params.page = 1
      this.getEmployeeList()
    },
    // 查询楼宇列表
    async getBuilding() {
      const res = await getBuildingAPI()
      this.buildingList = res.data
    },
    // 获取企业
    async getEnterprise() {
      const res = await getEnterpriseAPI()
      this.enterpriseList = res.data
    }
  }
}
</script>

<style lang="scss" scoped>

.employee-container {
  padding: 10px;
}

.search-container {
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgb(237, 237, 237, .9);
  ;
  padding-bottom: 20px;

  .search-label {
    width: 100px;
  }

  .search-main {
    width: 220px;
    margin-right: 10px;
  }
}
.create-container{
  margin: 10px 0px;
}

</style>

